<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>商品详情</title>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css"
          integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <style>
        .span_offset {
            margin-left: 20px;
        }
    </style>
</head>
<body>
<div class="container-fluid">
    <div class="row">
        <div class="col-sm-6 col-md-4 col-md-offset-3">
            <div class="thumbnail">
                <img th:src="${good.smallPic}" alt="...">
                <div class="caption">
                    <h3 th:text="${good.title}"></h3>
                    <p th:text="${good.introduction}"></p>
                    <p><span>库存</span><span class="span_offset" th:text="${good.stockCount}"></span></p>
                    <p><span>原价格</span><span class="span_offset" th:text="${good.price}"></span></p>
                    <p><span>秒杀价格</span><span class="span_offset" th:text="${good.costPrice}"></span></p>
                    <p><span>秒杀开始时间</span><span class="span_offset" th:text="${good.startTime}"></span></p>
                    <p><span>秒杀结束时间</span><span class="span_offset" th:text="${good.endTime}"></span></p>
                    <p><span>剩余时间</span><span id="timeString" class="span_offset" style="color:red"></span></p>
                    <p><a th:href="${'/order/saveOrder2/'+good.id}" class="btn btn-danger right" role="button">立即购买</a>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"
        integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
        crossorigin="anonymous"></script>
<script th:inline="javascript">
    $(document).ready(function () {
        //倒计时
        showTime();
    });

    function showTime() {
        //计算出剩余时间
        var endTime = new Date([[${good.endTime}]]).getTime();
        var nowTime = new Date().getTime();
        //剩余时间
        var secondes = Math.floor((endTime - nowTime) / 1000);

        var timer = setInterval(function () {
            if (secondes > 0) {
                //时间递减
                secondes = secondes - 1;
                $("#timeString").text(DateToStr(secondes));
            } else {
                clearInterval(timer); // 点击完成后关闭timer
            }
        }, 1000);
    }

    function DateToStr(allseconds) {
        //计算天数
        var days = Math.floor(allseconds / (60 * 60 * 24));

        //小时
        var hours = Math.floor((allseconds - (days * 60 * 60 * 24)) / (60 * 60));

        //分钟
        var minutes = Math.floor((allseconds - (days * 60 * 60 * 24) - (hours * 60 * 60)) / 60);

        //秒
        var seconds = (allseconds - (days * 60 * 60 * 24) - (hours * 60 * 60) - (minutes * 60)).toFixed(0);

        if (seconds < 10) {
            seconds = "0" + seconds;
        }
        //拼接时间
        var timString = "";
        if (days > 0) {
            timString = days + "天:";
        }
        return timString += hours + "小时:" + minutes + "分钟:" + seconds + "秒";
    }

</script>
</body>
</html>